package com.globallogic.gtalk.client;


import java.util.Collection;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class GtalkClient extends Activity {
	 	
	private static final String TAG = GtalkClient.class.getSimpleName();
	

	private EditText mEmailId;
	
	private EditText mPassword;
	
	private Button mLoginButton;
	
	    
	    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_screen);
        
        mEmailId = (EditText) this.findViewById(R.id.email);
        mPassword = (EditText) this.findViewById(R.id.password);
        mLoginButton = (Button) this.findViewById(R.id.loginbutton);
        
        mLoginButton.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				createXMPPConnection();
				
			}
        	
        });
        
        
        
    }
    
   
	private void createXMPPConnection(){
    	 // Create a connection
        
		ConnectionConfiguration connConfig =   new ConnectionConfiguration(Constants.HOST , Constants.PORT, Constants.SERVICE);
		 
        XMPPConnection connection = new XMPPConnection(connConfig);
        

        try {
        	Log.i(TAG, "Connecting to " + connection.getHost());
            connection.connect();
            Log.i(TAG, "Connected successfully ");
        } catch (XMPPException ex) {
            Log.e(TAG, "Failed to connect to " + connection.getHost());
            Log.e(TAG, ex.toString());
            SharedObject.setConnection(null);
        }
        
        try {
        	Log.i(TAG, "Logging in as " + mEmailId.getText().toString());
        	String user = mEmailId.getText().toString();
        	String pwd = mPassword.getText().toString();
            connection.login(user, pwd);
            Log.i(TAG, "Logged in successfully " );

            // Set the status to available
            Presence presence = new Presence(Presence.Type.available);
            connection.sendPacket(presence);
            SharedObject.setConnection(connection);
            
            
            //----------Print Roster
            
            Roster roster = connection.getRoster();
            Collection<RosterEntry> entries = roster.getEntries();
            for (RosterEntry entry : entries) {
                Log.i(TAG, entry.toString() + ":" + entry.getName());
            }
            
            ///---------
            //TODO: Call another activity here. 
            Intent i = new Intent();
            i.setClassName("com.globallogic.gtalk.client", "com.globallogic.gtalk.client.ChatListActivity");
            startActivity(i);
            
        } catch (XMPPException ex) {
        	Log.e(TAG, "Failed to log in as " + mEmailId.getText().toString());
            Log.e(TAG, ex.toString());
            SharedObject.setConnection(null);
        }
    }
    
    
    
}